package mylibrary.encoding;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;

public class Test1 {

	public static void main(String[] args) throws Exception {
		
		String file = "c:/test1.txt";
		String charest = "utf-8";  //gbk
		
		//write char to stream
		FileOutputStream outputStream = new FileOutputStream(file);
		OutputStreamWriter writer = new OutputStreamWriter(outputStream, charest);
		try{
			writer.write("这是要保存的中文字符");
		} finally {
			writer.close();
		}
		
		//read stream and convert to char
		FileInputStream inputStream = new FileInputStream(file);
		InputStreamReader reader = new InputStreamReader(inputStream, charest);
		StringBuffer buffer = new StringBuffer();
		char[] buf = new char[64];
		int count = 0;
		try {
			while((count=reader.read(buf)) != -1) {
				buffer.append(buf, 0, count);	
			}
			System.out.println(buffer);
		} finally {
			reader.close();
		}
	}
}
